﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Xml;
using imeem.api.util;

namespace imeem.api
{
	public enum UploadType
	{
		music,
		video,
		photo
	};
	
	public class MediaUpload
	{
		private static string mediaGetUploadTokenUri = @"http://www.api.imeem.com/api/xml/mediaGetUploadToken?apiKey={0}&version={1}&sig={2}";
		private static string apiUploadUri = @"http://upload.imeem.com/apiupload.aspx";
		private string apiKey = string.Empty;
		private string apiSecret = string.Empty;
		private CookieCollection cookies = null;
		
		public MediaUpload(string key, string secret, CookieCollection cookies)
		{
			this.apiKey = key;
			this.apiSecret = secret;
			this.cookies = cookies;
		}
		
		public static string GenerateMediaGetUploadTokenUri(string apiKey, string version, string sig)
		{
			return string.Format(mediaGetUploadTokenUri, apiKey, version, sig);
		}
		
		private string GetUploadToken()
		{
			string responseXml = string.Empty;
			Hashtable parameters = new Hashtable();
			parameters.Add("apiKey", this.apiKey);
			parameters.Add("version", "1.0");
			string sig = Util.GenerateSignature("mediaGetUploadToken", parameters, apiSecret);
			parameters.Add("sig", sig);
			string uri = GenerateMediaGetUploadTokenUri(parameters["apiKey"] as string, parameters["version"] as string , parameters["sig"] as string);
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
			request.CookieContainer = new CookieContainer();
			foreach (Cookie c in cookies)
			{
				request.CookieContainer.Add(c);
			}
			request.Timeout = 10000;
			HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			Encoding encoding = Encoding.GetEncoding(1252);
			StreamReader responseStream = new StreamReader(response.GetResponseStream());
			responseXml = responseStream.ReadToEnd();
			response.Close();
			responseStream.Close();
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(responseXml);
			return doc.SelectSingleNode("result").SelectSingleNode("result").InnerText;
		}
		
		public void InvokeUpload(string filename, UploadType type)
		{
			string boundry = DateTime.Now.Ticks.ToString("x");
			string contentType = "multipart/form-data; boundary=" + boundry;
			string accept = "text/*";
			string userAgent = "flash";
			string method = "POST";
			string token = GetUploadToken();
			ArrayList postParameters = new ArrayList();
			postParameters.Add(new PostParameter("Filename", PostParameter.GetOnlyFileName(filename)));
			postParameters.Add(new PostParameter("type", type.ToString()));
			postParameters.Add(new PostParameter("token", token));
			postParameters.Add(new PostParameter("NeatUpload_e205c58d3a3540bea350d073d9741896-UploadFile", PostParameter.GetOnlyFileName(filename), filename, PostParameterType.File));
			byte [] postBodyBuffer = Util.GeneratePostBodyBytes(postParameters, boundry);
			
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUploadUri);
			request.Timeout = System.Threading.Timeout.Infinite;
			request.Method = method;
			request.Accept = accept;
			request.ContentType = contentType;
			request.UserAgent = userAgent;
			request.KeepAlive = true;
			request.Headers.Set("Pragma", "no-cache");
			request.ContentLength = postBodyBuffer.Length;
			request.CookieContainer = new CookieContainer();
			foreach (Cookie c in cookies)
			{
				request.CookieContainer.Add(c);
			}
			Stream requestStream = request.GetRequestStream();
			requestStream.Write(postBodyBuffer, 0, postBodyBuffer.Length);
			requestStream.Close();
			HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			StreamReader responseStream = new StreamReader(response.GetResponseStream());
			string responseData = responseStream.ReadToEnd();
			response.Close();
			responseStream.Close();
		}
	}
}
